// setTimeout实现setInterval
function mySetinterval(fn, t) {
  let timer = null
  function interval() {
    fn()
    timer = setTimeout(interval, t)
  }
  interval()
  return {
    cancel: () => {
      clearTimeout(timer)
    },
  }
}

mySetinterval(() => {
  console.log(1)
}, 1000)

// 反过来用 setInterval 实现 setTimeout
function mySettimeout(fn, t) {
  const timer = setInterval(() => {
    clearInterval(timer)
    fn()
  }, t)
}

mySettimeout(() => {
  console.log(1)
}, 1000)
